package com.gzgy.ustore.api.web.controller.webservice;

import com.gzgy.ustore.api.common.util.EncryptionUtils;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.common.util.ParamUtil;
import com.gzgy.ustore.api.service.ApkService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import com.gzgy.ustore.api.web.handler.ApkUploadHandler;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Map;


@Controller
public class ApkController extends BaseController {

    @Autowired
    private ApkService apkService;

    @Autowired
    private ApkUploadHandler apkUploadHandler;


    @RequestMapping(value = "apkList",method = RequestMethod.POST)
    public String apkList(HttpServletRequest request , ModelMap modelMap) {
        //拿到 app 解密 string
        String dp = request.getAttribute("dp").toString();
        Map<String, String> map = ParamUtil.paramToMap(dp);
        String sn = map.get("sn");
        Assert.hasText(sn,"data_1 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        String timeStamp = map.get("timeStamp");
        Assert.hasText(sn,"data_2 "+I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(apkService.getApkList(sn,timeStamp)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "registerDeviceToken",method = RequestMethod.POST)
    public String registerDeviceToken(HttpServletRequest request ,ModelMap modelMap,String access_data) throws UnsupportedEncodingException, SQLException {
        //拿到 app 解密 string
        String dp = request.getAttribute("dp").toString();
        Map<String, String> map = ParamUtil.paramToMap(dp);
        String sn = map.get("sn");
        Assert.hasText(sn,"data_1 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        String timeStamp = map.get("timeStamp");
        Assert.hasText(sn,"data_2 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        String deviceToken = map.get("deviceToken");
        Assert.hasText(deviceToken,"data_3 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        modelMap.put("message",ResultUtil.getResultJson(apkService.registerDeviceToken(sn,timeStamp,deviceToken)));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "quickRegisterDevice",method = RequestMethod.POST)
    public String quickRegisterDevice(HttpServletRequest request ,ModelMap modelMap,String access_data) throws UnsupportedEncodingException {
       //拿到 app 解密 string
        String dp = request.getAttribute("dp").toString();
        modelMap.put("message",ResultUtil.getResultJson(apkUploadHandler.quickRegisterDevice(dp)));
        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "checkForRegister",method = RequestMethod.POST)
    public String checkForRegister(HttpServletRequest request ,ModelMap modelMap,String access_data) {
        //拿到 app 解密 string
        String dp = request.getAttribute("dp").toString();
        Map<String, String> map = ParamUtil.paramToMap(dp);
        String sn = map.get("sn");
        Assert.hasText(sn,"data_1 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        String timeStamp = map.get("timeStamp");
        Assert.hasText(sn,"data_2 "+I18nUtil.getMessage(-2,null,"zh_CN"));
        modelMap.put("message", ResultUtil.getResultJson(apkService.checkForRegister(sn,timeStamp)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "uploadApk", method = RequestMethod.POST)
    public String uploadApk(ModelMap modelMap, @RequestParam("file") MultipartFile file, String userId, String session, String companyId, String appCode) throws Exception {

        Assert.hasText(companyId, "companyId " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(appCode, "appCode " + I18nUtil.getMessage(-2, null, "zh_CN"));

        logger.info("upload apk start ");
        String result = apkUploadHandler.uploadApk(file, userId, companyId, appCode);
        logger.info("upload apk end");
        modelMap.put("message", result);

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "setAppInfo", method = RequestMethod.POST)
    public String setAppInfo(ModelMap modelMap, String userId, String session, String deviceType, String developer, String summaryDesc, String osInfo, String detailDesc, String updateDesc, String apkFileId) throws Exception {

        Assert.hasText(deviceType, "deviceType " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(apkFileId, "apkFileId " + I18nUtil.getMessage(-2, null, "zh_CN"));

        String result = apkService.addAppInfo(deviceType, developer, summaryDesc, osInfo, detailDesc, updateDesc, NumberUtils.toInt(apkFileId), userId);
        modelMap.put("message", ResultUtil.getResultJson(result));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "setAppDownloadRule", method = RequestMethod.POST)
    public String setAppDownLoadRule(ModelMap modelMap, String userId, String session, String dvcRule, String appCode, String downloadTimeRule, String updateRule,
                                     String downloadRule, String downloadTimeStart, String downloadTimeEnd, String companyId, String recommendRule) throws SQLException {

        Assert.hasText(appCode, "appCode " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(dvcRule, "dvcRule " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(downloadTimeRule, "downloadTimeRule " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(updateRule, " " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(downloadRule, "downloadRule " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(companyId, "companyId " + I18nUtil.getMessage(-2, null, "zh_CN"));
        Assert.hasText(recommendRule, "recommendRule " + I18nUtil.getMessage(-2, null, "zh_CN"));
        // 0 全部时间段
        if (!downloadTimeRule.equals("0")) {
            Assert.hasText(downloadTimeStart, "downloadTimeStart " + I18nUtil.getMessage(-2, null, "zh_CN"));
            Assert.hasText(downloadTimeEnd, "downloadTimeEnd " + I18nUtil.getMessage(-2, null, "zh_CN"));
        }

        String result = apkService.setAppDownloadRule(userId, dvcRule, appCode, downloadTimeRule, updateRule, downloadRule, downloadTimeStart, downloadTimeEnd,
                companyId, recommendRule);

        modelMap.put("message",ResultUtil.getResultJson(result));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "apkListByWeb",method = RequestMethod.POST)
    public String apkListByWeb(ModelMap modelMap,String userId,String session,String companyId,String index,String page,String appCode,String dvcType) {

        Assert.hasText(companyId,"companyId " +I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(index,"index " +I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(page,"page " +I18nUtil.getMessage(-2,null,"zh_CN"));


        modelMap.put("message", ResultUtil.getResultJson(apkService.apkListByWeb(companyId,index,page,appCode,dvcType)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "deleteApk",method = RequestMethod.POST)
    public String deleteApk(ModelMap modelMap,String userId,String session,String apkId) throws SQLException {
        Assert.hasText(apkId,"apkId "+ I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(apkService.deleteApk(apkId,userId)));

        return SHOW_JSON_NAME;
    }
}
